<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>修改密码 - [[${configs.title}]]</title>
    <link rel="stylesheet" href="/statics/layui-v2.8.15/css/layui.css">
    <link rel="stylesheet" href="/statics/css/admin/common.css">
</head>

<body>
<!-- 内容主体区域 -->
<div class="my-form">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>修改密码</legend>
    </fieldset>
    <form class="layui-form edit-password">
        <div class="layui-form-item">
            <label class="layui-form-label">旧密码</label>
            <div class="layui-input-inline">
                <input type="password" name="oldPassword" lay-verify="required" autocomplete="off" style="width:400px" placeholder="请输入旧密码" id="old-password" class="layui-input my-text">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-inline">
                <input type="password" name="newPassword" lay-verify="required" autocomplete="off" style="width:400px" placeholder="请输入新密码" id="new-password" class="layui-input my-text">
            </div>
            <div class="layui-form-mid layui-word-aux">密码必须包含数字、大小字母和特殊字符，长度要求在8到16位之间</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">重复密码</label>
            <div class="layui-input-inline">
                <input type="password" name="repeatPassword" lay-verify="required" autocomplete="off" style="width:400px" placeholder="请输入重复密码" id="repeat-password" class="layui-input my-text">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="password-form">保 存</button>
            </div>
        </div>
    </form>
</div>
<script src="/statics/layui-v2.8.15/layui.js"></script>
<script src="/statics/js/md5.js"></script>
<script>
    //JavaScript代码区域
    layui.use('form', function () {
        let $ = layui.jquery, form = layui.form;
        //监听提交
        form.on('submit(password-form)', function (data) {
            data = data.field;
            const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[`~!@#$%^&*()_\-+=|\\;':",./<>?])[A-Za-z\d`~!@#$%^&*()_\-+=|\\;':",./<>?]{8,16}$/;
            if (data.oldPassword === data.newPassword) {
                layer.msg('新密码和旧密码不能相同');
                return false;
            } else if (!reg.test(data.newPassword)) {
                layer.msg('密码必须包含数字、大小字母和特殊字符，长度要求在8到16位之间');
                return false;
            } else if (data.newPassword !== data.repeatPassword) {
                layer.msg('两次密码输入不一致');
                return false;
            } else {
                data.oldPassword = hex_md5(data.oldPassword);
                data.newPassword = hex_md5(data.newPassword);
                data.repeatPassword = hex_md5(data.repeatPassword);
                $.post("/admin/user/edit-password", data, function (data) {
                    console.log(data);
                    if (data.code === undefined || data.code === 0) {
                        layer.msg('密码修改成功');
                        setTimeout(function () {
                            parent.location.href = '/admin/user/logout';
                        }, 1000);
                    } else {
                        layer.msg(data.error);
                    }
                });
                return false;
            }
        });
    });
</script>
</body>

</html>